Date.prototype.format = function(fmt) { //author: meizz
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };

    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }

    return fmt;
}

// Trim whitespace from left and right sides of s.
function trim(s) {
    return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

function isEmpty(str){
    if(str == null || trim(str) == ""){
        return true;
    }
    return false;
}

/**
 * Query city list by province, and then generate options for select element.
 * IF throws exception, then forward to index page
 *
 * @param province
 */
function queryCityList(selectId, provinceCode, defaultSelected, promptText, _callback) {
    $("#" + selectId).empty();
    $("#" + selectId).append("<option value='-1'>" + promptText + "</option>");
    if(provinceCode === '-1') {
        $("#" + selectId).combobox({
            onChange: function (n, o) {
                if(typeof(_callback) != "undefined" && _callback != undefined) {
                    _callback();
                }
            }
        });
        return;
    }

    $.ajax({
        url: "/area/queryCityList.json",
        type: "post",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json",
        data: {provinceCode : provinceCode},
        success: function (result) {
            if(result.errorCode == 0) {
                var cityList = result.data;
                for (var i = 0; i < cityList.length; i++) {
                    var item = cityList[i]
                    $("#" + selectId).append("<option value='" + item.code + "'>" + item.name + "</option>");
                }
            }
            $("#" + selectId).combobox({
                onChange: function (n, o) {
                    if(typeof(_callback) != "undefined" && _callback != undefined) {
                        _callback();
                    }
                },
                value : defaultSelected
            });
        },
        error : function() {
            $("#" + selectId).combobox({
                onChange: function (n, o) {
                    if(typeof(_callback) != "undefined" && _callback != undefined) {
                        _callback();
                    }
                }
            });
        }
    });
}

function imageFormat(value,row,index) {
    if(isEmpty(value)) {
        return "";
    } else {
        return "<img src='" + value + "' style='width: 50px; height: 50px; margin-top: 5px;'>";
    }
}

function statusFormat(value, row, index) {
    if(value == 1) {
        return "已启用";
    } else {
        return "没启用";
    }
}

function dateFormat(value, row, index) {
    var theDate = new Date(value);
    return theDate.format("yyyy-MM-dd hh:mm:ss");
}

function linkFormat(value,row,index) {
    return "<a href='" + value + "' target='_blank'><font color='blue'>" + value + "</font></a>";
}